﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WpfApp1.Models;

namespace WpfApp1.Controls
{
    /// <summary>
    /// 连接点层控件（具体的连接点）
    /// </summary>
    public class PortControl : ContentControl
    {
        /// <summary>
        /// 设置DiagramCanvas层中的初始连接点属性
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            DiagramCanvas canvas = GetDesignerCanvas(this);
            if (canvas != null)
            {
                canvas.SourcePort = this;
            }
        }

        private DiagramCanvas GetDesignerCanvas(DependencyObject element)
        {
            while (element != null && !(element is DiagramCanvas))
                element = VisualTreeHelper.GetParent(element);

            return element as DiagramCanvas;
        }

        public PortBase Info
        {
            get
            {
                if (Content is PortBase port)
                    return port;

                return this.DataContext as PortBase;
            }
        }
    }
}
